home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7695 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: electra.saaf.se!not-for-mail
  2. From: pausch@electra.saaf.se (Paul Schlyter)
  3. Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
  4. Subject: Re: Access carry flag from C
  5. Date: 25 Feb 1996 09:50:01 +0100
  6. Organization: Svensk Amat|rAstronomisk F|rening
  7. Message-ID: <4gp7rq$i63@electra.saaf.se>
  8. References: <Dn1C9z.DGv.0.net@indra.com>
  9. NNTP-Posting-Host: electra.saaf.se
  10.  
  11. In article <Dn1C9z.DGv.0.net@indra.com>,
  12. Steve Sullivan <sullivan@indra.com> wrote:
  13.  
  14. > Is it possible to determine if a fixed point overflow has
  15. > occurred from within C?  For example:
  16. >    i = j + k;
  17. >    if (overflowed) ....;
  18.  
  19. Only using inline assembly, or some other way of directly accessing
  20. the CPU registers, i.e. the method would be very implementation
  21. dependent.
  22.  
  23. One example: Using Borlands C/C++ on PC's one could do like this:
  24.  
  25.     i = j + k;
  26.     if ( _FLAGS & mask ) .....;
  27.  
  28. where _FLAGS is a reserved word which directly maps into the CPU
  29. flag register, and mask is an appropriate mask value set by the
  30. programmer, depending on what kind of overflow he wants to detect.
  31. If he wants to detect signed overflow, he should check the OVERFLOW
  32. flag, if he wants to detect unsigned overflow he should check the
  33. CARRY flag.
  34.  
  35. C has no portable way to check for integer (?) overflow.  Unless you'd
  36. want to do stuff like:
  37.  
  38.    if ( ((double) j) + ((double) k) > MAX_INT ) ......
  39.  
  40. -- 
  41. ----------------------------------------------------------------
  42. Paul Schlyter,  Swedish Amateur Astronomer's Society (SAAF)
  43. Grev Turegatan 40,  S-114 38 Stockholm,  SWEDEN
  44. e-mail:  pausch@saaf.se        psr@home.ausys.se
  45.